package hotLeetcode;

import java.util.Arrays;

public class H15Leetcode238 {

    //暴力超时
    public int[] productExceptSelf(int[] nums) {

        int len = nums.length;
        int[] answer = new int[len];

        for (int i = 0; i < len; i++) {
            answer[i] = 1;


            for (int j = 0; j < len; j++) {
                if (j == i) continue;
                answer[i] *= nums[j];

            }

        }

        return answer;

    }

    public int[] productExceptSelf2(int[] nums) {

        int len = nums.length;
        int[] answer = new int[len];

        answer[0] = 1;

        for (int i = 1; i < len; i++) {
            answer[i] = answer[i - 1] * nums[i - 1];
        }
        int temp = 1;
        for (int i = len - 2; i >= 0; i--) {
            temp *= nums[i + 1];
            answer[i] *= temp;
        }

        return answer;

    }



    public static void main(String[] args) {
        int[] nums = {-1,1,0,-3,3};
        H15Leetcode238 h15Leetcode238 = new H15Leetcode238();
        System.out.println(Arrays.toString(h15Leetcode238.productExceptSelf2(nums)));

    }

}
